﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Xenta.Enums;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    public class ExportStockEntries : ExportOperation,
        ISecuredOperation
    {
        #region Properties

        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
            }
        }

        #endregion

        #region Utilities

        protected override void Work(IUnitOfWork uow, out IEnumerable<IDictionary<String, String>> result)
        {
            result = uow.Execute(new SearchStockEntries()).Result
                .Select(x => new Dictionary<String, String>
                {
                    {"ItemID", x.Item.EntityID.ToString()},
                    {"AdjustmentMin", x.AdjustmentMin.ToString("F2", CultureInfo.InvariantCulture)},
                    {"AdjustmentMax", x.AdjustmentMax.ToString("F2", CultureInfo.InvariantCulture)},
                    {"Reserve", x.Reserve.ToString("F2", CultureInfo.InvariantCulture)},
                    {"ExhaustionThreshold", x.ExhaustionThreshold.ToString("F2", CultureInfo.InvariantCulture)},
                    {"Flags", x.Flags.ToString()}
                }).ToList();
        }

        #endregion
    }

    public class ExportSalesOptions : ExportOperation,
        ISecuredOperation
    {
        #region Properties

        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
            }
        }

        #endregion

        #region Utilities

        protected override void Work(IUnitOfWork uow, out IEnumerable<IDictionary<String, String>> result)
        {
            var ret = new List<IDictionary<String, String>>();
            var languages = uow.Execute(new SearchLanguages()).Result;

            foreach(var i in uow.Execute(new SearchSalesOptions()).Result)
            {
                var obj = new Dictionary<String, String>
                {
                    {"EntityID", i.EntityID.ToString()},
                    {"Price", i.Price.ToString("F2", CultureInfo.InvariantCulture)},
                    {"Code", String.Format("{0}:{1}", i.CodeName, i.CodeValue)},
                    {"Flags", i.Flags.ToString()}
                };
                foreach(var l in languages)
                {
                    var t = i.Translation(l.EntityID);
                    obj.Add(l.Code, t != null ? String.Format("{0}:{1}", t.Name, t.Value) : String.Empty);
                }
                ret.Add(obj);
            }
            result = ret;
        }

        #endregion
    }

    public class ExportSalesItems : ExportOperation,
        ISecuredOperation
    {
        #region Properties

        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SALES");
            }
        }

        #endregion

        #region Utilities

        protected override void Work(IUnitOfWork uow, out IEnumerable<IDictionary<String, String>> result)
        {
            int? sellerID = null;
            int index = 0;
            int count = 100;
            var sortDir = SortDirection.Ascending;
            var sortBy = SalesItemSortBy.ID;

            if(Properties.ContainsKey("sellerID"))
                sellerID = Properties["sellerID"].ParseAs<Int32?>();
            if(Properties.ContainsKey("index"))
                index = Properties["index"].ParseAs(0);
            if(Properties.ContainsKey("count"))
                count = Properties["count"].ParseAs(100);
            if(Properties.ContainsKey("sortDir"))
                sortDir = Properties["sortDir"].ParseAs(SortDirection.Ascending);
            if(Properties.ContainsKey("sortBy"))
                sortBy = Properties["sortBy"].ParseAs(SalesItemSortBy.ID);

            result = uow.Execute(new SearchSalesItems
            {
                SellerID = sellerID,
                Index = index,
                Count = count,
                SortDir = sortDir,
                SortBy = new []
                {
                    sortBy
                }
            }).Result.Select(i => new Dictionary<String, String>
            {
                {"EntityID", i.EntityID.ToString()},
                {"SKU", i.SKU},
                {"SellerID", i.Seller.EntityID.ToString()},
                {"CatalogItemID", i.CatalogItem.EntityID.ToString()},
                {"Price", i.Price.ToString("F2", CultureInfo.InvariantCulture)},
                {"Rules", i.Rules.Select(x => x.EntityID.ToString()).Merge(",")},
                {"Options", i.Options.Select(x => x.EntityID.ToString()).Merge(",")},
                {"AvailableFrom", i.AvailableFrom.ToString(CultureInfo.InvariantCulture)},
                {"AvailableTo", i.AvailableTo.ToString(CultureInfo.InvariantCulture)},
                {"Flags", i.Flags.ToString()}
            }).ToList();
        }

        #endregion
    }
}
